﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using RegisterationCommon.Common;
using RegisterationCommon.School;
using RegisterationCommon.Student;
using System.Data;
using RegisterationCommon.Education;
using System.IO;
namespace RegisterationSystem.Space_Education
{
    public partial class students_undistributed : System.Web.UI.Page
    {
        public string ShcJson = "";
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                ShcJson = BaseLibrary.GetSclJson();
                switch (Request.Form["action"])
                {
                    case "upd":
                        UpdSclCount();
                        break;
                    case "del":
                        DelStus();
                        break;
                    case "pswdreset":
                        UpdStudentPswdReset();
                        break;
                    //case "derive_excelList":
                    //    derive_excelList();
                    //    break;
                    //case "derive_excelListAll":
                    //    derive_excelListAll();
                    //    break;
                }
                Bind();
            }
        }
        //绑定学校
        void Bind()
        {
            //DataTable dt = DBAction.GetDTFromSQL("select * from SchoolInfo");
            //DataRow[] drs = dt.Select();
            //ddlSchoolInfo.Items.Add(new ListItem("全部", "99"));
            //foreach (DataRow dr in drs)
            //{
            //    ListItem item = new ListItem();
            //    item.Text += dr["Name"].ToString();
            //    item.Value = dr["id"].ToString();
            //    ddlSchoolInfo.Items.Add(item);
            //}
            //Repeater1.DataSource = DBAction.GetDTFromSQL("select * from Community where id>1");
            //Repeater1.DataBind();
        }
        //删除学生
        void DelStus()
        {
            //获取修改ID和学校ID
            string idlist = Request.Form["id"];
            string[] ids = idlist.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
            for (int i = 0; i < ids.Length; i++)
            {
                StudentInfo s = new StudentInfo();
                s.ID = Convert.ToInt32(ids[i]);
                s.IsDelete = true;
                s.SetDeleteStatic();
            }
        }
        //分配学生
        void UpdSclCount()
        {
            //获取修改ID和学校ID
            string idlist = Request.Form["id"];
            string countlist = Request.Form["count"];
            string isadjustmentList = Request.Form["isadjustment"];
            string[] ids = idlist.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
            string[] counts = countlist.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
            string[] isadjustment = isadjustmentList.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
            int n = 0;
            for (int i = 0; i < ids.Length; i++)
            {
                if (isadjustment[i].ToString() == "不服从-1n")
                {
                    n = n + 1;
                    continue;
                }
                StudentInfo s = new StudentInfo();
                s.ID = Convert.ToInt32(ids[i]);
                s.SchID = Convert.ToInt32(counts[i]);

                s.Ext2 = "0";//0为手动分配

                s.SetSclID();
            }
            if (n > 0)
            {
                if (ids.Length > 1)
                {
                    Response.Clear();
                    Response.Write("有" + n + "名C类学生不服从调剂");
                    Response.End();
                }
                else
                {
                    Response.Clear();
                    Response.Write("该名C类学生不服从调剂");
                    Response.End();
                }
            }
            else
            {
                Response.Clear();
                Response.Write("-1");
                Response.End();
            }
        }
        //根据id重置学生帐号密码
        void UpdStudentPswdReset()
        {
            string id = Request.Form["id"].ToString();
            string StudentPswd = Request.Form["StudentPswd"].ToString();
            StudentInfo s = new StudentInfo();
            s.MdfAccountPwd(Convert.ToInt64(id), BaseLibrary.ConvertToMD5(StudentPswd.ToLower()));
        }

        //void DirectoryInfoList()
        //{
        //    int day = 0;
        //    DirectoryInfo theFolder = new DirectoryInfo(Server.MapPath(@"..\StudentInfoToExcel\"));
        //    FileInfo[] dirInfo = theFolder.GetFiles();
        //    //遍历文件夹
        //    foreach (FileInfo NextFile in dirInfo)
        //    {
        //        string filename = NextFile.Name;
        //        string[] Date = filename.Split('_');
        //        if (Date.Length > 1)
        //        {
        //            string strTimeNow = DateTime.Now.ToString("yyyyMMdd");
        //            int d1 = int.Parse(Date[1]);
        //            int d2 = int.Parse(strTimeNow);
        //            int ND = d2 - d1;
        //            day = ND;
        //            if (day >= 1)
        //            {
        //                File.Delete(Server.MapPath(@"..\StudentInfoToExcel\" + filename));
        //            }
        //        }
        //    }
        //}
        //void derive_excelList()
        //{
        //    DirectoryInfoList();
        //    //获取修改ID
        //    string stuID = "";
        //    string idlist = Request.Form["id"];
        //    string[] ids = idlist.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
        //    for (int i = 0; i < ids.Length; i++)
        //    {
        //        if (i != (ids.Length - 1))
        //        {
        //            stuID = stuID + ids[i] + " or id=";
        //        }
        //        else
        //        {
        //            stuID = stuID + ids[i];
        //        }
        //    }
        //    DataTable dt = DBAction.GetDTFromSQL("Select * From " + ETableName.Students + " where id=" + stuID);
        //    EduExcelHelper excel = new EduExcelHelper();
        //    string Toexcelpath = excel.SCexcel(dt, Server.MapPath(@"..\StudentInfoToExcel\"));
        //    Response.Clear();
        //    string result = "..\\" + "StudentInfoToExcel\\" + Toexcelpath;
        //    Response.Write(result);
        //    Response.End();
        //}



        //void derive_excelListAll()
        //{
        //    DirectoryInfoList();
        //    //获取修改ID
        //    string where = Request.Form["where"];
        //    string typeid = Request.Form["typeid"];
        //    EduExcelHelper excel = new EduExcelHelper();
        //    DataTable dt = excel.GetUndistributedStuList(where, typeid);
        //    string Toexcelpath = excel.SCexcel(dt, Server.MapPath(@"..\StudentInfoToExcel\"));
        //    Response.Clear();
        //    string result = "..\\" + "StudentInfoToExcel\\" + Toexcelpath;
        //    Response.Write(result);
        //    Response.End();
        //}
    }
}